/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.mq.main;

import cn.ac.iie.di.wx.mq.common.RuntimeEnv;
import static cn.ac.iie.di.wx.mq.common.RuntimeEnv.redisVos;
import cn.ac.iie.di.wx.mq.configuration.Configuration;
import cn.ac.iie.di.wx.mq.httpservice.HttpSender;
import cn.ac.iie.di.wx.mq.redis.RedisThread;
import cn.ac.iie.di.wx.mq.utils.RedisUtil;
import cn.ac.iie.di.wx.mq.vo.RedisVo;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class MonitorCplBy400 {

    static Logger logger = Logger.getLogger(MonitorCplBy400.class.getName());

    public static void main(String[] args) {
        try {
            init();
            RedisUtil redisUtil = new RedisUtil("10.144.32.21:26379;10.144.32.22:26379", "mymaster", 4);
            RedisThread redisThread = new RedisThread(redisUtil, redisVos);
            Thread t = new Thread(redisThread);
            t.start();
            logger.info("start redis ,host 10.144.32.21:26379;10.144.32.22:26379,master :mymaster, dbname:4");
            String root = System.getProperty("user.dir");
            String absolutePath = root + "/" + (String) (RuntimeEnv.getParam(RuntimeEnv.cplLogFileName));//筛选出OCRThemes的日志
            logger.info("从" + absolutePath + "恢复开始");
            FileReader reader = new FileReader(absolutePath);
            BufferedReader br = new BufferedReader(reader);

            String str = "";
            Map<String, String> requestMap = new HashMap<>();
            while ((str = br.readLine()) != null) {
                Thread.sleep(10);//一秒钟发一个。。。
                String request = str.split("receive OCR request:")[1];
                JSONObject requestJson = JSONObject.parseObject(request);
                String opid = requestJson.getString("opid");//存redis里，将关键字存value里面
                requestMap.put(opid, request);
                logger.info("opid = " + request);
            }
            //获取400的列表，从中筛选opid
            String responseabsolutePath = root + "/" + (String) (RuntimeEnv.getParam(RuntimeEnv.responseFileName));
            logger.info(responseabsolutePath);
            FileReader rspreader = new FileReader(responseabsolutePath);
            BufferedReader rspbr = new BufferedReader(rspreader);
            Map<String, String> responseMap = new HashMap<>();
            while ((str = rspbr.readLine()) != null) {
                Thread.sleep(10);//一秒钟发一个。。
                logger.info(str);
                String response = str.split("OCR response is sucess:")[1];
                JSONObject responseJson = JSONObject.parseObject(response);
                String opid = responseJson.getString("opid");//存redis里，将关键字存value里面
                responseMap.put(opid, response);
                logger.info("opid = " + response);
            }
            logger.info("开始对比");
            Map<String, String> handleMap = new HashMap<>();
            for (Map.Entry<String, String> entrySet : responseMap.entrySet()) {
                String key = entrySet.getKey();
                String value = entrySet.getValue();
                if (requestMap.containsKey(key)) {
                    handleMap.put(key, requestMap.get(key));
                    logger.info("opid = " + key + ",request is " + requestMap.get(key) + ",response is " + value);
                }
            }
            logger.info("开始发送烽火");
            for (Map.Entry<String, String> entrySet : handleMap.entrySet()) {
                Thread.sleep(1000);//一秒钟发一个。。。
                String key = entrySet.getKey();
                String value = entrySet.getValue();
                logger.info("key = " + key + ";value = " + value);
                logger.info("重新发送，响应为：" + HttpSender.sendStringByUrl(value, (String) RuntimeEnv.getParam(RuntimeEnv.corURL)));
                boolean offerFlag = RuntimeEnv.redisVos.offer(new RedisVo(key, "1"));
                logger.info("入redis队列 opid:" + key + ",offer flag is " + offerFlag);
                if (!offerFlag) {
                    try {
                        //加入失败
                        Thread.sleep(100);
                        offerFlag = RuntimeEnv.redisVos.offer(new RedisVo(key, "1"));
                        logger.info("入redis队列 opid:" + key + ",offer flag is " + offerFlag);
                    } catch (InterruptedException ex) {
                        logger.error(ex.getMessage(), ex);
                    }
                }
            }
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
    }

    public static void init() throws Exception {
        String configurationFileName = "configuration.properties";
        Configuration conf = Configuration.getConfiguration(ClassLoader.getSystemClassLoader().getResource(configurationFileName).getPath());
        if (conf == null) {
            logger.error("reading " + configurationFileName + " is failed.");
        }
        try {
            RuntimeEnv.initialize(conf);///参数，
            logger.info("init successfully");
        } catch (Exception ex) {
            logger.error("initializng runtime enviroment is failed for" + ex.getMessage());
        }
    }
}
